package com.safeneeds.linguimana.entity.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.transform.Transformers;

import com.safeneeds.linguimana.dto.IpDataStorePort;
import com.safeneeds.linguimana.entity.PortGlutLog;
import com.safeneeds.linguimana.entity.PortGlutStar;
import com.safeneeds.linguimana.entity.PortVisitData;
import com.safeneeds.linguimana.entity.dao.IPortGlutLogDao;
import com.safeneeds.util.base.BaseHibernateDao;
import com.safeneeds.util.date.DateUtil;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;

public class PortGlutLogDao extends BaseHibernateDao<PortGlutLog, Serializable> implements IPortGlutLogDao {
	/**
	 * 得到访问日志表中的访问统计数据包装在PortVisitData类中
     * @return PortVisitData
	 */
	public PortVisitData getPortVisitData() {
		String nowtime=DateUtil.getDateByYMD();
		String nowmonth=DateUtil.getDateByYM();
		String sql="select * from "
				+ "(select count(*)\"TODAYNUM\" from port_glut_log "
				+ "where to_char(time,'yyyymmdd')='"+nowtime+"'),"
				+ "(select count(*)\"YESTERDAYNUM\" from port_glut_log "
				+ "where to_char(time+1,'yyyymmdd')='"+nowtime+"'), "
				+ "(select count(*)\"MONTHNUM\" from port_glut_log "
				+ "where to_char(time,'yyyymm')='"+nowmonth+"'), "
				+ "(select count(*)\"TODAYNEWSNUM\" from port_news "
				+ "where to_char(update_time,'yyyymmdd')='"+nowtime+"'), "
				+ "(select count(*) \"SUMNUM\" from port_glut_log ),"
				+ "(select count(*)\"NEWSSUMNUM\" from port_news)";
		/*List<PortVisitData> list = this.getSQLQuery(sql, PortVisitData.class);
		PortVisitData visits=list.get(0);*/
		List list = null;
		Session session = currentSession();
		//List list=getSQLQuery(sql);
		list=session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		Map<String, Object> map = (Map<String, Object>) list.get(0);
		PortVisitData visits=new PortVisitData();
		visits.setTodayNum(map.get("TODAYNUM").toString());
		visits.setYesterdayNum(map.get("YESTERDAYNUM").toString());
		visits.setMonthNum(map.get("MONTHNUM").toString());
		visits.setTodayNewsNum(map.get("TODAYNEWSNUM").toString());
		visits.setNewsSumNum(map.get("NEWSSUMNUM").toString());
		visits.setSumNum(map.get("SUMNUM").toString());
		return visits;
	
	}
	@Override
	public PortGlutLog add(PortGlutLog log) {
		return this.saveEntity(log);
	}
	@Override
	public PageInfo listLogPageDao(PageInfo pageInfo,
			IpDataStorePort ipDataStorePort) {
		// TODO Auto-generated method stub
		String hsql = "SELECT * FROM PORT_GLUT_LOG ";
		String countSql = "SELECT COUNT(*) FROM PORT_GLUT_LOG";
		
		String whereSql = "";
		String orderSql = " ORDER BY TIME desc";
		
		if(ipDataStorePort !=null){		
			whereSql += " where 1 = 1";
			//获取ip
			if(!StringUtil.isEmpty(ipDataStorePort.getIp())){
				whereSql += " and ip like '%" + StringUtil.addQuote(ipDataStorePort.getIp()) + "%'";
			}
			//时间
			if(!StringUtil.isEmpty(ipDataStorePort.getTimestart()) && !StringUtil.isEmpty(ipDataStorePort.getTimeend())){//有开始时间和结束时间
				if(DateUtil.compareDateYYYYMMDD(ipDataStorePort.getTimestart(), ipDataStorePort.getTimeend())){//开始时间大于结束时间
					whereSql += " and TIME >= to_timestamp('" + ipDataStorePort.getTimeend() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and TIME <= to_timestamp('" + ipDataStorePort.getTimestart() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
				}else{
					whereSql += " and TIME <= to_timestamp('" + ipDataStorePort.getTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and TIME >= to_timestamp('" + ipDataStorePort.getTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
				}
			}else if(!StringUtil.isEmpty(ipDataStorePort.getTimestart())){//只有开始时间
				whereSql += " and TIME >= to_timestamp('" + ipDataStorePort.getTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
			}else if(!StringUtil.isEmpty(ipDataStorePort.getTimeend())){//只有结束时间
				whereSql += " and TIME <= to_timestamp('" + ipDataStorePort.getTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
			}
		}
		pageInfo.setEntityClass(PortGlutLog.class);
		pageInfo.setListHbSql(hsql+whereSql+orderSql);
		pageInfo.setTotalCountHbSQL(countSql+whereSql);
		return pageInfo;
	}
	@Override
	public void insertLog(PortGlutLog glutLog) {
		// TODO Auto-generated method stub
		this.saveEntity(glutLog);
	}
	@Override
	public boolean existIpFromLog(String tempip,String todayTime) {
		// TODO Auto-generated method stub
		String sql="select count(*) from PORT_GLUT_LOG where ip= '"+tempip+"' and to_char(time,'yyyymmdd') = '" + todayTime+"'";
		return "0".equals(this.getSQLQuery(sql).get(0)+"");
	}

}
